package co.com.smart.eagle.session;

import javax.naming.InitialContext;
import javax.naming.NamingException;

import co.com.smart.eagle.bean.application.ApplicationBeanLocal;
import co.com.smart.eagle.bean.resources.ResourcesBeanLocal;
import co.com.smart.eagle.bean.rol.RolBeanLocal;
import co.com.smart.eagle.bean.user.UserBeanLocal;
import co.com.smart.eagle.persistence.entity.ApplicationsFacadeLocal;
import co.com.smart.eagle.persistence.entity.ResourcesFacadeLocal;
import co.com.smart.eagle.persistence.entity.RolFacadeLocal;
import co.com.smart.eagle.persistence.entity.UserFacadeLocal;

public class DelegateContextEJB {

	/**
	 * Beans EJB delegate
	 */
	private static UserBeanLocal userBeanLocal;
	private static RolBeanLocal rolBeanLocal;
	private static ApplicationBeanLocal applicationBeanLocal;
	private static ResourcesBeanLocal resourcesBeanLocal;

	/**
	 * Beans Facade persistentes
	 */
	private static UserFacadeLocal userFacadeLocal;
	private static RolFacadeLocal rolFacadeLocal;
	private static ApplicationsFacadeLocal applicationsFacadeLocal;
	private static ResourcesFacadeLocal   resourcesFacadeLocal;

	/**
	 * BEANS
	 */
	public static UserBeanLocal getUserBeanLocal() {

		if (userBeanLocal == null) {
			try {
				userBeanLocal = (UserBeanLocal) new InitialContext()
						.lookup("java:global/SmartEagleEAR/SmartEagleEJB/UserBean");
			} catch (NamingException e) {
				System.out.println("Error recuperando el servicio "
						+ e.getMessage());
			}
		}

		return userBeanLocal;

	}

	public static RolBeanLocal getRolBeanLocal() {

		if (rolBeanLocal == null) {
			try {
				rolBeanLocal = (RolBeanLocal) new InitialContext()
						.lookup("java:global/SmartEagleEAR/SmartEagleEJB/RolBean");
			} catch (NamingException e) {
				System.out.println("Error recuperando el servicio "
						+ e.getMessage());
			}
		}

		return rolBeanLocal;

	}

	public static ApplicationBeanLocal getApplicationBeanLocal() {
		if (applicationBeanLocal == null) {
			try {
				applicationBeanLocal = (ApplicationBeanLocal) new InitialContext()
						.lookup("java:global/SmartEagleEAR/SmartEagleEJB/ApplicationBean");
			} catch (NamingException e) {
				System.out.println("Error recuperando el servicio "
						+ e.getMessage());
			}
		}
		return applicationBeanLocal;
	}

	public static ResourcesBeanLocal getResourcesBeanLocal() {
		if(resourcesBeanLocal == null){
			try {
				resourcesBeanLocal = (ResourcesBeanLocal) new InitialContext()
						.lookup("java:global/SmartEagleEAR/SmartEagleEJB/ResourcesBean");
			} catch (NamingException e) {
				System.out.println("Error recuperando el servicio "
						+ e.getMessage());
			}
		}
		return resourcesBeanLocal;
	}

	/**
	 * FACADES
	 */

	public static UserFacadeLocal getUserFacadeLocal() {

		if (userFacadeLocal == null) {
			try {
				userFacadeLocal = (UserFacadeLocal) new InitialContext()
						.lookup("java:global/SmartEagleEAR/SmartEagleEJB/UserFacade");
			} catch (NamingException e) {
				System.out.println("Error recuperando el servicio "
						+ e.getMessage());
			}
		}

		return userFacadeLocal;
	}

	public static RolFacadeLocal getRolFacadeLocal() {

		if (rolFacadeLocal == null) {
			try {
				rolFacadeLocal = (RolFacadeLocal) new InitialContext()
						.lookup("java:global/SmartEagleEAR/SmartEagleEJB/RolFacade");
			} catch (NamingException e) {
				System.out.println("Error recuperando el servicio "
						+ e.getMessage());
			}
		}

		return rolFacadeLocal;
	}

	public static ApplicationsFacadeLocal getApplicationsFacadeLocal() {
		if(applicationsFacadeLocal == null){
			try {
				applicationsFacadeLocal = (ApplicationsFacadeLocal) new InitialContext()
						.lookup("java:global/SmartEagleEAR/SmartEagleEJB/ApplicationsFacade");
			} catch (NamingException e) {
				System.out.println("Error recuperando el servicio "
						+ e.getMessage());
			}
		}
		return applicationsFacadeLocal;
	}

	public static ResourcesFacadeLocal getResourcesFacadeLocal() {
		if(resourcesFacadeLocal == null){
			try {
				resourcesFacadeLocal = (ResourcesFacadeLocal) new InitialContext()
						.lookup("java:global/SmartEagleEAR/SmartEagleEJB/ResourcesFacade");
			} catch (NamingException e) {
				System.out.println("Error recuperando el servicio "
						+ e.getMessage());
			}
		}
		return resourcesFacadeLocal;
	}


}
